package com.magic.smartcity.view.more.adapters

import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.opengl.Visibility
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.widget.AppCompatDrawableManager
import androidx.databinding.DataBindingUtil
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.magic.smartcity.R
import com.magic.smartcity.base.BaseApplication
import com.magic.smartcity.beans.BusLineBean
import com.magic.smartcity.beans.BusLineStopBean
import com.magic.smartcity.databinding.ItemBusHomeListBinding

/**
 * @description:
 * @create: Create by Magic on 2022/2/23.
 */
class BusLineListAdapter : RecyclerView.Adapter<BusLineListAdapter.ItemHolder>() {
    private val list: MutableList<BusLineBean> = mutableListOf()
    private val stopList: MutableList<BusLineStopBean> = mutableListOf()
    fun setData(data: List<BusLineBean>, stationList: List<BusLineStopBean>) {
        list.clear()
        list.addAll(data)
        stopList.clear()
        stopList.addAll(stationList)
        notifyDataSetChanged()
    }

    class ItemHolder(binding: ItemBusHomeListBinding) : RecyclerView.ViewHolder(binding.root) {
        val binding = binding
    }

    private val TAG: String = "BusLineListAdapter"
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemHolder {
        val inflate = DataBindingUtil.inflate<ItemBusHomeListBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_bus_home_list,
            parent,
            false
        )
        return ItemHolder(inflate)
    }

    override fun onBindViewHolder(holder: ItemHolder, position: Int) {
        holder.binding.busLineDetail = list[position]
        holder.binding.btnExpand.setOnClickListener {
            holder.binding.expandContent.visibility =
                if (holder.binding.expandContent.visibility == View.GONE) View.VISIBLE else View.GONE
            holder.binding.btnExpand.setImageDrawable(
                if (holder.binding.expandContent.visibility == View.GONE)
                    AppCompatResources.getDrawable(
                        holder.itemView.context,
                        R.drawable.ic_expand_right
                    )
                else
                    AppCompatResources.getDrawable(
                        holder.itemView.context,
                        R.drawable.ic_expand_bottom
                    )
            )
        }
        val lineStopList =
            stopList.filter { it.linesId == list[position].id }.sortedBy { it.sequence }
        val stationAdapter = BusLineListStopListAdapter(lineStopList)
        holder.binding.stationRy.layoutManager =
            LinearLayoutManager(BaseApplication.mContext, LinearLayoutManager.VERTICAL, false)
        holder.binding.stationRy.adapter = stationAdapter

        holder.binding.root.setOnClickListener{
            val bundle = Bundle()
            bundle.putSerializable("busLineDetail",list[position])
            Navigation.findNavController(it).navigate(R.id.action_busHomeFragment_to_busDetailFragment,bundle)
        }
    }

    override fun getItemCount() = list.size
}